﻿/**************************************************************************
创建时间:	2020/5/14
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.Win.Controls;
using ZhCun.Win.Extend;

namespace ZhCun.Win.ExtendGridView
{
    public partial class FrmGridColumnEdit : FrmBaseEdit
    {
        public FrmGridColumnEdit()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 上级grid的行号，用于自动赋值列顺序
        /// </summary>
        public int DgvRowCount { set; get; }

        public Form OwnerForm { set; get; }

        public string SearchFormName { set; get; }

        void Init()
        {
            coBoxDataType.Items.Clear();
            coBoxDataType.Items.AddRange(new string[] { "string", "int", "decimal", "bool", "datetime" });
            coBoxDataType.SelectedIndex = 0;

            coBoxColumnType.Items.Clear();
            var colTypes = Enum.GetNames(typeof(DgvColumnType));
            coBoxColumnType.Items.AddRange(colTypes);
            coBoxColumnType.SelectedIndex = 0;

            //对齐方式
            coBoxAlignment.Items.Clear();
            var alTypes = Enum.GetNames(typeof(DataGridViewContentAlignment));
            coBoxAlignment.Items.AddRange(alTypes);
            coBoxAlignment.SelectedIndex = 0;

            coBoxColAutoSizeMode.Items.Clear();
            var autoMode = Enum.GetNames(typeof(DataGridViewAutoSizeColumnMode));
            coBoxColAutoSizeMode.Items.AddRange(autoMode);
            coBoxColAutoSizeMode.SelectedIndex = 0;
        }

        private void FrmGridColumnEdit_Load(object sender, EventArgs e)
        {
            if (OwnerForm != null)
            {
                txtFormName.Text = SearchFormName; //OwnerForm.GetType().ToString();
                btnFindGridView.Visible = true;
                txtFormName.ReadOnly = true;
                coBoxDgvName.Focus();
            }
            if (EditArg.ClearId)
            {
                txtColumnIndex.Value = DgvRowCount + 1; 
            }

        }

        private void btnFindGridView_Click(object sender, EventArgs e)
        {
            if (OwnerForm == null) return;
            coBoxDgvName.Items.Clear();
            OwnerForm.FindControl(FindAfter, FindMatch);
            if (coBoxDgvName.Items.Count > 0) coBoxDgvName.SelectedIndex = 0;
            bool FindAfter(Control c)
            {
                coBoxDgvName.Items.Add(c.Name);
                return false;
            }

            bool FindMatch(Control c)
            {
                return c is DataGridView;
            }
        }
    }
}
